/**    
 * @{#} RemoteImageDownloader.java Create on 2013-7-3 上午9:10:14    
 *        
 * @author Evan
 *
 * @email evan0502@qq.com
 *
 * @version 1.0    
 */
package com.smiier.skin.utils;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.evan.common.connection.Net;
import com.evan.common.connection.remotefile.RemoteImageDownloader;
import com.evan.common.handler.ImageHandler;
import com.evan.common.utils.FilesUtil;
import com.smiier.skin.constant.Constant;

public class RemoteVoiceDownloader {

	public static void downloadVoice(final String path,
			final ImageHandler handler) {
		if (!CommonUtility.isNull(path)) {
			String name = RemoteImageDownloader.getFileNameString(path, false);
			if (FilesUtil.compare(name)) {
				Message msg = Message.obtain();
				msg.obj = FilesUtil.getRootPath() + name;
				handler.sendMessage(msg);
			} else {
				new Thread() {

					@Override
					public void run() {
						// TODO Auto-generated method stub

						loadVoice(path, handler);
					}
				}.start();
			}
		}
	}

	public static void loadVoice(String path, Handler handler) {
		try {
			String name = RemoteImageDownloader.getFileNameString(path, false);
			Net net = new Net();
			byte[] data = net.downloadResource(Constant.REQUEST_VOICE_URL
					+ path);
			FilesUtil.saveImage(name, data);
			Message m = Message.obtain();
			m.obj = FilesUtil.getRootPath() + name;
			handler.sendMessage(m);
			data = null;
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
}
